/*
 */

package com.railway.msg.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.org.rapid_framework.page.Page;

import com.railway.common.base.BaseManager;
import com.railway.common.base.EntityDao;
import com.railway.common.util.StaticMap;
import com.railway.files.dao.IFilesDao;
import com.railway.files.model.Files;
import com.railway.msg.constants.IConstant;
import com.railway.msg.dao.IMsgReciveDao;
import com.railway.msg.dao.IMsgSendDao;
import com.railway.msg.model.MsgRecive;
import com.railway.msg.model.MsgSend;
import com.railway.msg.service.IMsgSendManager;
import com.railway.msg.vo.query.MsgSendQuery;

/**
 * @author jiangcheng
 * @version 1.0
 * @since 1.0
 */

@Service("msgSendManager")
@Transactional
public class MsgSendManagerImpl extends BaseManager<MsgSend, java.lang.String>	implements IMsgSendManager {
	private IFilesDao filesDao;
	private IMsgReciveDao msgReciveDao;
	private IMsgSendDao msgSendDao;

	/** 增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,请注意大小写 */
	public void setMsgSendDao(IMsgSendDao dao) {
		this.msgSendDao = dao;
	}

	public EntityDao getEntityDao() {
		return (EntityDao) this.msgSendDao;
	}

	@Transactional(readOnly = true)
	public Page findPage(MsgSendQuery query) {
		
		Page p = null;
	try{	p = msgSendDao.findPage(query);
		List list = p.getResult();
		if(list != null){
			for(int i = 0 ; i < list.size(); i++){
				MsgSend msgsend = (MsgSend)list.get(i);
				msgsend.setCreator(msgsend.getCreator() == null?null:StaticMap.getUserValue(msgsend.getCreator()));

			}
		}
	}catch(Exception ex){
		ex.printStackTrace();
	}
		return p;
	}

	@Transactional(readOnly = true)
	public MsgSend getById(java.lang.String id) {
		return super.getById(id);
	}

	@Transactional
	public void removeById(java.lang.String id) {
		super.removeById(id);
	}

	/**
	 * 保存一个发送消息,包含以下动作
	 * 
	 * 1.将发送消息的内容等主体部分保存到“发送表” 2.将附件保存到"文件表",其中objid字段保存步骤中的发送消息表的相应id
	 * 3.根据将接收者基本信息保存到接收者表,其中Msgid字段保存步骤1中的发送消息表的相应id
	 */
	@Transactional
	public void save(MsgSend entity) {

	
		// 在这里,要取得附件的id
		String uploadedfileid = entity.getUploadedfileid();
		entity.setCreateTime(new java.sql.Timestamp(System.currentTimeMillis()));
		//entity.setCreateTimeString(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(System.currentTimeMillis())));
        //根据当前用户 id获得当前用户名
		//String current_user_name = StaticMap.getUserValue( entity.getCreator());
	   //以用户名代替id
//		entity.setCreator(current_user_name);
        //保存发送消息
		super.save(entity);

		//上传附件是可选的,如果上传了附件的话
		if (uploadedfileid != null && !"".equals(uploadedfileid)) {
			// 用uploadedfileid来更新附件表的objid为MsgSendid
			Files fs = (Files) this.getFilesDao().getById(uploadedfileid);			

			fs.setObjId(entity.getId());
			this.getFilesDao().update(fs);
		}

		// 保存接收信息
		if (entity.getTargetids() == null) {
			return;
		}
		String[] userids = entity.getTargetids().split(",");
		if (userids == null) {
			return;
		}
		IMsgReciveDao recivedao = getMsgReciveDao();
		for (int k = 0; k < userids.length; k++) {
            String uid= userids[k];
            if( uid == null || "".equals(uid)) continue;
			MsgRecive msgrecive = new MsgRecive();
            //此时不放发送者,发送者是从数据库里查出来的
			msgrecive.setMsgId(entity.getId());
		 //   msgrecive.setReadTime(new java.sql.Timestamp(System.currentTimeMillis()));
		//	msgrecive.setReadTimeString(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
			msgrecive.setRecivor(uid);
			msgrecive.setStatus(IConstant.MSG_STATUS_UNREAD);//未阅读
			recivedao.save(msgrecive);
		}

	}

	/** 更新数据 */
	@Transactional
	public void update(MsgSend entity) {
		super.update(entity);
	}

	@Transactional(readOnly = true)
	public boolean isUnique(MsgSend entity, String uniquePropertyNames) {
		return super.isUnique(entity, uniquePropertyNames);
	}

	@Transactional(readOnly = true)
	public List<MsgSend> findAll() {
		return super.findAll();
	}

	@Transactional
	public void saveOrUpdate(MsgSend entity) {
		super.saveOrUpdate(entity);
	}

	public void setMsgReciveDao(IMsgReciveDao d) {
		this.msgReciveDao = d;
	}

	public IMsgReciveDao getMsgReciveDao() {
		return msgReciveDao;
	}

	public IFilesDao getFilesDao() {
		return filesDao;
	}

	public void setFilesDao(IFilesDao filesDao) {
		this.filesDao = filesDao;
	}

}
